/*
* DIOdrv.c
*
* Created: 15.02.2013 20:51:39
*  Author: victor
*/
#include <Basetypes.h>
#include <DIOdrv.h>
#include <avr/io.h>



/* MACRO definitions */


void DIODrv_Init ( void )
{
	/* All output pins are set to 1 and inputs set to 0 */
	DDRD |= ( 1 << DIODRV_C_PLAY_PAUSE_U8 ) | ( 1 << DIODRV_C_NEXT_U8 ) | ( 1 << DIODRV_C_PREVIOUS_U8 ) | ( 1 << DIODRV_C_PHONE_U8 ) ;
	/* Pin 0 of port B will control the power supply of the BTT */
	DDRB |= ( 1 << DIODRV_C_PWRBTT_U8 ) | ( 1 << DIODRV_C_BTT_ON_OFF_U8 );
}

void DIODrv_SetPin ( EN_CommonDefinition_CommandsEnum command )
{
	switch ( command )
	{
		case SUPPLY_RST_EN:
			{
				PORTB |= ( 1 << DIODRV_C_PWRBTT_U8 ); /* The pin for Supply on/off button */
				break;
			}
			
		case PHONE_ANS_HUP_EN:
			{
				PORTD |= ( 1 << DIODRV_C_PHONE_U8 ); /* The pin for Phone action answer/ hangup button button */
				break;
			}
			
		case PLAY_TRACK_EN:
			{
				PORTD |= ( 1 << DIODRV_C_PLAY_PAUSE_U8 ); /* The pin for Play/pause button */
				break;
			}
			
		case PAUSE_TRACK_EN:
			{
				PORTD |= ( 1 << DIODRV_C_PLAY_PAUSE_U8 ); /* The pin for Play/pause button */
				break;
			}
			
		case NEXT_TRACK_EN:
			{
				PORTD |= ( 1 << DIODRV_C_NEXT_U8 ); /* The pin for Next button */
				break;
			}
			
		case PREVIOUS_TRACK_EN:
			{
				PORTD |= ( 1 << DIODRV_C_PREVIOUS_U8 ); /* The pin for Previous button */
				break;
			}
			
		case POWER_ON_EN:
			{
				PORTB |= ( 1 << DIODRV_C_BTT_ON_OFF_U8 ); /* The pin for Power ON/OFF button (the same as for Power OFF) */
				break;
			}
			
		case ALL_EN:
			{
				PORTD |= ( 1 << DIODRV_C_PLAY_PAUSE_U8 ) | ( 1 << DIODRV_C_NEXT_U8 ) | ( 1 << DIODRV_C_PREVIOUS_U8 ) | ( 1 << DIODRV_C_PHONE_U8 ) ; /* For debug and bash port manipulation purpose */
				PORTB |= ( 1 << DIODRV_C_PWRBTT_U8 ) | ( 1 << DIODRV_C_BTT_ON_OFF_U8 ) ;
				break;
			}
			
		default:
			{
				break;
			}
	}
}

void DIODrv_ClearPin ( EN_CommonDefinition_CommandsEnum command )
{
	switch ( command )
	{
		case SUPPLY_RST_EN:
			{
				PORTB &= ~ ( 1 << DIODRV_C_PWRBTT_U8 ); /* The pin for Supply on/off button */
				break;
			}
			
		case PHONE_ANS_HUP_EN:
			{
				PORTD &= ~ ( 1 << DIODRV_C_PHONE_U8 ); /* The pin for Phone action answer/ hangup button button */
				break;
			}
			
		case PLAY_TRACK_EN:
			{
				PORTD &= ~ ( 1 << DIODRV_C_PLAY_PAUSE_U8 ); /* The pin for Play/pause button */
				break;
			}
			
		case PAUSE_TRACK_EN:
			{
				PORTD &= ~ ( 1 << DIODRV_C_PLAY_PAUSE_U8 ); /* The pin for Play/pause button */
				break;
			}
			
		case NEXT_TRACK_EN:
			{
				PORTD &= ~ ( 1 << DIODRV_C_NEXT_U8 ); /* The pin for Next button */
				break;
			}
			
		case PREVIOUS_TRACK_EN:
			{
				PORTD &= ~ ( 1 << DIODRV_C_PREVIOUS_U8 ); /* The pin for Previous button */
				break;
			}
			
		case POWER_ON_EN:
			{
				PORTB &= ~ ( 1 << DIODRV_C_BTT_ON_OFF_U8 ); /* The pin for Power ON/OFF button */
				break;
			}
			
		case ALL_EN:
			{
				
				PORTD &= ~( ( 1 << DIODRV_C_PLAY_PAUSE_U8 ) | ( 1 << DIODRV_C_NEXT_U8 ) | ( 1 << DIODRV_C_PREVIOUS_U8 ) | ( 1 << DIODRV_C_PHONE_U8 ) ); /* For debug and bash port manipulation purpose */
				PORTB &= ~( ( 1 << DIODRV_C_PWRBTT_U8 ) | ( 1 << DIODRV_C_BTT_ON_OFF_U8 ) );
				break;
			}
			
		default:
			{
				break;
			}
	}
}
u8 DIODrv_GetPinState ( EN_CommonDefinition_LEDsEnum pin )
{
	u8 tmp_retVal_u8 = 0;
	
	switch ( pin )
	{
		case RED_LED_EN:
			{
				/* read the 2nd bit 0b00000010=0x02 */
				if ( 0x02 & PINC )
				{
					tmp_retVal_u8 = 0x01;
				}
				
				break;
			}
			
		case GREEN_LED_EN:
			{
				/* read the 1st bit 0b00000001=0x01 */
				if ( 0x01 & PINC )
				{
					tmp_retVal_u8 = 0x01;
				}
				
				break;
			}
			
		case BLUE_LED_EN:
			{
				/* read the 3rd bit 0b00000100=0x04 */
				if ( 0x04 & PINC )
				{
					tmp_retVal_u8 = 0x01;
				}
				
				break;
			}
			
		default:
			{
				break;
			}
	}
	
	return tmp_retVal_u8;
}

void DIODrv_TogglePin ( EN_CommonDefinition_CommandsEnum command )
{
	switch ( command )
	{
		case REVERSE_EN:
			{
				PORTD ^= ( 1 << DIODRV_C_PREVIOUS_U8 );  /* The pin for Previous button */
				break;
			}
			
		case FAST_FWD_EN:
			{
				PORTD ^= ( 1 << DIODRV_C_NEXT_U8 );  /* The pin for Next button */
				break;
			}
			
		default:
			{
				break;
			}
	}
}